package com.neuedu.xkxt.service.impl;

import com.neuedu.xkxt.dao.CourseDao;
import com.neuedu.xkxt.dao.CourseMapper;
import com.neuedu.xkxt.dao.ScoreStatsMapper;
import com.neuedu.xkxt.dao.StudentCourseRelDao;
import com.neuedu.xkxt.model.Course;
import com.neuedu.xkxt.model.ScoreDetails;
import com.neuedu.xkxt.model.Student;
import com.neuedu.xkxt.model.StudentCourseRelExample;
import com.neuedu.xkxt.service.CourseService;
import com.neuedu.xkxt.vo.PageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {
    @Resource
    private CourseDao courseDao;
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private StudentCourseRelDao studentCourseRelDao;

    @Resource
    private ScoreStatsMapper scoreStatsMapper;
    @Override
    public int addCourse(Course course) {
        return courseDao.insertSelective(course);
    }

    @Override
    public int updateCourse(Course course) {
        return courseDao.updateByPrimaryKeySelective(course);
    }

    @Override
    public int deleteCourse(int id) {
        return courseDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageVO<Student> page(int pageNo, int pageSize, int courseId) {
        List<Student> data=courseMapper.findStudentByCourseId(courseId,(pageNo-1)*pageSize,pageSize);
        int count=courseMapper.countByPage(courseId);

        PageVO<Student> pageVO=new PageVO<>();
        pageVO.setData(data);
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setPageCount(count/pageSize+(count%pageSize==0?0:1));
        return pageVO;
    }

    @Override
    public int delStudentCourseRel(int courseId, int studentId) {
        StudentCourseRelExample example=new StudentCourseRelExample();
        StudentCourseRelExample.Criteria criteria=example.createCriteria();
        criteria.andCidEqualTo(courseId);
        criteria.andSidEqualTo(studentId);


        return studentCourseRelDao.deleteByExample(example);
    }

    @Override
    public void saveScore(int stuId, int courseId, int score) {
        studentCourseRelDao.updateScore(stuId,courseId,score);
    }

    @Override
    public List<ScoreDetails> scoreDetails() {

        return scoreStatsMapper.getCourseScoreStats();
    }
}
